package me.zhengjie.modules.report.service.impl;

import me.zhengjie.modules.report.domain.ReportConfig;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.report.repository.ReportConfigRepository;
import me.zhengjie.modules.report.service.ReportConfigService;
import me.zhengjie.modules.report.service.dto.ReportConfigDTO;
import me.zhengjie.modules.report.service.dto.ReportConfigQueryCriteria;
import me.zhengjie.modules.report.service.mapper.ReportConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author vivi207
* @date 2019-07-25
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ReportConfigServiceImpl implements ReportConfigService {

    @Autowired
    private ReportConfigRepository reportConfigRepository;

    @Autowired
    private ReportConfigMapper reportConfigMapper;

    @Override
    public Object queryAll(ReportConfigQueryCriteria criteria, Pageable pageable){
        Page<ReportConfig> page = reportConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(reportConfigMapper::toDto));
    }

    @Override
    public Object queryAll(ReportConfigQueryCriteria criteria){
        return reportConfigMapper.toDto(reportConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public ReportConfigDTO findById(Long id) {
        Optional<ReportConfig> reportConfig = reportConfigRepository.findById(id);
        ValidationUtil.isNull(reportConfig,"ReportConfig","id",id);
        return reportConfigMapper.toDto(reportConfig.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReportConfigDTO create(ReportConfig resources) {
        return reportConfigMapper.toDto(reportConfigRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ReportConfig resources) {
        Optional<ReportConfig> optionalReportConfig = reportConfigRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalReportConfig,"ReportConfig","id",resources.getId());

        ReportConfig reportConfig = optionalReportConfig.get();
        // 此处需自己修改
        resources.setId(reportConfig.getId());
        reportConfigRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        reportConfigRepository.deleteById(id);
    }
}